package com.yanqu.xiuxian.crosssystem.manger.elementalbonds.data;

import com.yanqu.xiuxian.config.elementalbonds.ElementalBondsCardConfig;
import com.yanqu.xiuxian.config.elementalbonds.ElementalBondsConfigMgr;
import com.yanqu.xiuxian.config.elementalbonds.ElementalBondsParam;
import com.yanqu.xiuxian.config.elementalbonds.enums.ElementalBondsHonorMomentType;

import java.util.*;

public class ElementalBondsPlayerData {

    private ElementalBondsMatchData elementalBondsMatchData;

    // 默认0的
    private long score;
    private long sendExpressionTime;
    private long stepOutTime;
    private boolean ready = true;

    // 游戏开始后才有
    private int step;
    private ElementalBondsCardData elementalBondsCardData = null;
    private List<ElementalBondsSkillData> elementalBondsSkillDataList = null;
    private boolean firstOperate = false;                       // 是否先手，目前只用于记录日志

    // 皮肤相关
    private int boardSkinId = 0;                                // 皮肤id
    private int animationId = 0;                                // 动画id
    private int effectId = 0;                                   // 特效id

    // 单局成就统计
    private Map<ElementalBondsHonorMomentType, Long> achieveStatisticalMap = new HashMap<>();

    public static ElementalBondsPlayerData createData(ElementalBondsMatchData userMatchData) {
        if (userMatchData == null) {
            return null;
        }

        ElementalBondsCardConfig cardConfig = ElementalBondsConfigMgr.getInstance().getCardConfigMap().get(userMatchData.getCardId());
        List<ElementalBondsSkillData> skillList = new ArrayList<>();
        for (Integer skill : userMatchData.getSkills()) {
            skillList.add(new ElementalBondsSkillData(skill, ElementalBondsParam.getInstance().getSkillIdUseLimit()));
        }

        ElementalBondsPlayerData userGameData = new ElementalBondsPlayerData();
        userGameData.setElementalBondsMatchData(userMatchData);
        userGameData.setScore(0);
        userGameData.setSendExpressionTime(0);
        userGameData.setElementalBondsCardData(new ElementalBondsCardData(userMatchData.getCardId(), 0, cardConfig == null ? 0 : cardConfig.getCd()));
        userGameData.setElementalBondsSkillDataList(skillList);
        userGameData.setBoardSkinId(userMatchData.getBoardSkinId());
        userGameData.setAnimationId(userMatchData.getAnimationId());
        userGameData.setEffectId(userMatchData.getEffectId());

        Set<ElementalBondsHonorMomentType> oneGameElementalBondsHonorMomentType = ElementalBondsHonorMomentType.getOneGameElementalBondsHonorMomentType();
        for (ElementalBondsHonorMomentType elementalBondsHonorMomentType : oneGameElementalBondsHonorMomentType) {
            userGameData.initAchieveStatistical(elementalBondsHonorMomentType);
        }

        return userGameData;
    }

    public void reduceStep() {
        this.step = this.step - 1;
    }

    public void addStep(int addNum, int maxNum) {
        step = Math.min(step + addNum, maxNum);
    }

    public void addScore(long addValue) {
        this.score += addValue;
        addAchieveStatistical(ElementalBondsHonorMomentType.OneGameScore, addValue);
        addAchieveStatistical(ElementalBondsHonorMomentType.OneActiveScore, addValue);
    }

    public void initAchieveStatistical(ElementalBondsHonorMomentType elementalBondsHonorMomentType) {
        achieveStatisticalMap.put(elementalBondsHonorMomentType, 0L);
    }

    public void addAchieveStatistical(ElementalBondsHonorMomentType elementalBondsHonorMomentType, long statisticalValue) {
        long currentStatisticalValue = achieveStatisticalMap.getOrDefault(elementalBondsHonorMomentType, 0L);
        long newStatisticalValue = elementalBondsHonorMomentType.isCumulative()
                ? currentStatisticalValue + statisticalValue  // 如果是累积类型，累加统计值
                : Math.max(currentStatisticalValue, statisticalValue); // 非累积类型时，取较大值
        achieveStatisticalMap.put(elementalBondsHonorMomentType, newStatisticalValue); // 更新统计值
    }

    public ElementalBondsMatchData getElementalBondsMatchData() {
        return elementalBondsMatchData;
    }

    public void setElementalBondsMatchData(ElementalBondsMatchData elementalBondsMatchData) {
        this.elementalBondsMatchData = elementalBondsMatchData;
    }

    public long getScore() {
        return score;
    }

    public void setScore(long score) {
        this.score = score;
    }

    public long getSendExpressionTime() {
        return sendExpressionTime;
    }

    public void setSendExpressionTime(long sendExpressionTime) {
        this.sendExpressionTime = sendExpressionTime;
    }

    public long getStepOutTime() {
        return stepOutTime;
    }

    public void setStepOutTime(long stepOutTime) {
        this.stepOutTime = stepOutTime;
    }

    public boolean isReady() {
        return ready;
    }

    public void setReady(boolean ready) {
        this.ready = ready;
    }

    public int getStep() {
        return step;
    }

    public void setStep(int step) {
        this.step = step;
    }

    public ElementalBondsCardData getElementalBondsCardData() {
        return elementalBondsCardData;
    }

    public void setElementalBondsCardData(ElementalBondsCardData elementalBondsCardData) {
        this.elementalBondsCardData = elementalBondsCardData;
    }

    public List<ElementalBondsSkillData> getElementalBondsSkillDataList() {
        return elementalBondsSkillDataList;
    }

    public void setElementalBondsSkillDataList(List<ElementalBondsSkillData> elementalBondsSkillDataList) {
        this.elementalBondsSkillDataList = elementalBondsSkillDataList;
    }

    public boolean isFirstOperate() {
        return firstOperate;
    }

    public void setFirstOperate(boolean firstOperate) {
        this.firstOperate = firstOperate;
    }

    public int getBoardSkinId() {
        return boardSkinId;
    }

    public void setBoardSkinId(int boardSkinId) {
        this.boardSkinId = boardSkinId;
    }

    public int getAnimationId() {
        return animationId;
    }

    public void setAnimationId(int animationId) {
        this.animationId = animationId;
    }

    public int getEffectId() {
        return effectId;
    }

    public void setEffectId(int effectId) {
        this.effectId = effectId;
    }

    public Map<ElementalBondsHonorMomentType, Long> getAchieveStatisticalMap() {
        return achieveStatisticalMap;
    }

    public void setAchieveStatisticalMap(Map<ElementalBondsHonorMomentType, Long> achieveStatisticalMap) {
        this.achieveStatisticalMap = achieveStatisticalMap;
    }

}
